package twelfth.对象序列化;

import java.io.*;

public class Transient声明之后再读取对象 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ser();
        dser();
    }

    public static void ser() throws IOException {
        File file = new File("D:\\WorkSpaces\\JavaDevelopBase_start_329\\src\\twelfth\\ObjectOutputStream.txt");
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(new 类中的name属性不希望被序列化("to be a better man", 88));
        oos.close();
    }

    public static void dser() throws IOException, ClassNotFoundException {
        File file = new File("D:\\WorkSpaces\\JavaDevelopBase_start_329\\src\\twelfth\\ObjectOutputStream.txt");
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Object obj = ois.readObject();
        ois.close();
        System.out.println(obj);
    }
}
/**
 * 因为name被transient声明，所以name为null，表示内容没有被序列化下来，这样对象在反序列化之后，输出的姓名的默认值就是null
 */
/*
D:\WorkSpaces\Java\jdk-21\bin\java.exe "-javaagent:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\lib\idea_rt.jar=57069:D:\Software\IntelliJ IDEA\IntelliJ IDEA 2023.3.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath D:\WorkSpaces\JavaDevelopBase_start_329\out\production\JavaDevelopBase_start_329 twelfth.对象序列化.Transient声明之后再读取对象
类中的name属性不希望被序列化{name='null', age=88}

Process finished with exit code 0
 */
